<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/12/7
 * Time: 10:23
 */

namespace api\storage\service;


use api\storage\common\Constant;
use api\storage\model\PurchaseDetailModel;
use api\storage\model\ReelsModel;
use api\storage\model\SubMaterielModel;
use think\Cache;

class MaterielService extends BaseService
{

    public function __construct()
    {
    }

    /**
     * 修改物料类型
     *
     * @param $materielId
     * @param $type
     * @param $storageUuid
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function modifyType($materielId, $type,$storageUuid)
    {
        $reelsModel = new ReelsModel();
        $materiel = self::getMaterielCache($materielId);
        if ($this->isEmpty($materiel)) {
            return $this->resultTemplate(-2);
        }
        $reel = $reelsModel->findOneReelIdByMaterielIdAndUuid($materielId,$storageUuid);
        //该materielId 没有在 reelId表 才能进行类型修改
        if ($this->isNotEmpty($reel)){
            return $this->resultTemplate(-1);
        }
        $subMaterielModel = new SubMaterielModel();
        $res = $subMaterielModel->updateByMaterielId(['shelves_type'=>$type],$materielId);
        if ($res !== false) {
            //清除缓存。
            self::evictMaterielCache($materielId);
            return $this->resultTemplate(1);
        }
        return $this->resultTemplate(0);
    }

    /**
     * 获取物料的缓存数据
     *
     * @param $materielId
     * @return array|false|mixed|\PDOStatement|string|\think\Model|null
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getMaterielCache($materielId)
    {
        if (empty($materielId)) {
            return null;
        }
        $materiel = Cache::get(Constant::CACHE_PREFIX_MATERIEL . $materielId);
        //没有缓存，则取缓存
        if (empty($materiel)) {
            $materielModel = new SubMaterielModel();
            //先去materiel表查找最小包装数据
            $materiel = $materielModel->findByMaterielId($materielId);
            if (empty($materiel) || empty($materiel->getData())) {
                return null;
            }
            $materiel = $materiel->toArray();
            Cache::set(Constant::CACHE_PREFIX_MATERIEL . $materielId, $materiel);
        }
        return $materiel;
    }

    /**
     * 删除缓存
     * @param $materielId
     */
    private static function evictMaterielCache($materielId){
        Cache::rm(Constant::CACHE_PREFIX_MATERIEL . $materielId);
    }
    /**
     * 获取物料的数量
     *
     * @param string $subMaterielId
     * @param string $storageUuid
     * @return int|mixed
     */
    public static function getSumCount($subMaterielId = '',$storageUuid)
    {
        if (empty($subMaterielId)) {
            return 0;
        }
        $reelModel = new ReelsModel();
        $res = $reelModel->countMaterielNumber($subMaterielId,$storageUuid);
        return $res > 0 ? $res : 0;
    }


    /**
     * @param string $subMaterielId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getTypeAndMinQuantity($subMaterielId = '')
    {
        $type = 'C';
        $minQuantity = 0;
        if (empty($subMaterielId)) {
            return [$type, $minQuantity];
        }
        $materielModel = new SubMaterielModel();
        //先去materiel表查找最小包装数据
        $materiel = $materielModel->findByMaterielId($subMaterielId);
        $type = $materiel->getData('shelves_type');
        $minQuantity = $materiel->getData('min_quantity');
        //如果类型不是A,直接返回
        if ($type != 'A') {
            return [$type, $minQuantity];
        }
        if ($minQuantity == 0) {
            //如果最小包装数量为0的话，去采购表统计一下最小包装数量
            $purchaseModel = new PurchaseDetailModel();
            $minQuantity = $purchaseModel->countMaterielNumber($subMaterielId);
            //如果得到了最小包装数量，那么要更新到materiel表中
            if ($minQuantity != 0) {
                $materielModel->updateByMaterielId(['min_quantity' => $minQuantity], $subMaterielId);
                return [$type, $minQuantity];
            } else {
                return [$type, $minQuantity];
            }
        } else {
            return [$type, $minQuantity];
        }
    }
}